package com.zking.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * String工具包
 * @author ZC
 *
 */
public class StringUtils {

	/**
	 * 日期格式化 日期--->String
	 * 
	 * @param str
	 * @param pattern
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		SimpleDateFormat df = new SimpleDateFormat();
		df.applyPattern(pattern);
		return df.format(date);
	}

	/**
	 * 字符串化的日期转化为Date类型   string--->Date
	 * @param date
	 * @param pattern
	 * @return  字符串对应的date类型
	 * @throws Exception 
	 */
	public static Date stringToDate(String date, String pattern) throws Exception{
		SimpleDateFormat df = new SimpleDateFormat();
		df.applyPattern(pattern);
		try {
			return df.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			throw new Exception("字符串:"+date+"不能转化为Date");
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
